package test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import thread.PropertiesUtil;

public class AutoInsert extends Thread{
	private boolean stop = false;
	public void run(){ 
		while(!stop){ 
		   Calendar cal=Calendar.getInstance(); 
		   int a=cal.get(Calendar.SECOND); 
		   if(a%1==0){
             if(a%5==0)
                 stopPolling();
             else{
		        String driverName = PropertiesUtil.getValue("driverName");
		  	  	String dbURL = PropertiesUtil.getValue("dbURL");
		  	    String userName =PropertiesUtil.getValue("userName");   
		  	    String userPwd = PropertiesUtil.getValue("userPwd");
  	   
  	   ResultSet rs = null;
  	   Statement statement = null;
       Connection dbConn=null;  
       Random rand=new Random();
       PreparedStatement pStatement = null; 	 
       try { 
    try {
			Class.forName(driverName);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
  	   dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
  	      int db=rand.nextInt(100);
  	      String location=null;
  	   Date now = new Date();  
  	  java.sql.Timestamp CreateTime= new java.sql.Timestamp(now.getTime());
  	  
  	 
  	    String strSql = "insert into noise(LOCATION,db,TimeStamp) values(?,?,?);";
		pStatement = dbConn.prepareStatement(strSql);
		pStatement.setString(1, location);
		pStatement.setFloat(2, db);
		pStatement.setTimestamp(3, CreateTime);
		pStatement.executeUpdate();
		
		/*strSql = "select id,LOCATION,db,TimeStamp from noise";
		statement=dbConn.createStatement();
		rs = statement.executeQuery(strSql);
	    System.out.println("id\t\tLOCATION\t\tdb\t\tTimeStamp");
		while(rs.next()){
			System.out.print(rs.getInt("id")+"\t\t");
			System.out.print(rs.getString("LOCATION")+"\t\t");
			System.out.print(rs.getFloat("db")+"\t\t");
			Timestamp timestamp=rs.getTimestamp("TimeStamp");
			Date date=new Date(timestamp.getTime());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			System.out.print(sdf.format(date)+"\t");
			System.out.println();
		}*/
		
		} catch (SQLException sqlE) {
			sqlE.printStackTrace();

		} finally {
			System.out.println("释放所有资源....");
			 try {
				
               if (dbConn != null)
                      dbConn.close();//关闭连接
               dbConn = null; //促使GC尽快回收资源
        } catch (Exception ex) {
               ex.printStackTrace();
        }  
		}
                   }
		      }
		    }
		}
		public void stopPolling(){
			this.stop = true;
		}
		      public static void main(String[] args) {
		    	  Runnable mt1=new AutoInsert();
				   Thread tr = new Thread(mt1);
				     tr.start(); 
		      }/**/
} 
